<?php
namespace app\index\model;

use think\Model;

class Blw extends Model
{
	/**
	 * 分页查询频道直播观看详情数据
	 * @param   $channelId      直播间频道
	 * @param   $currentDay     查询日期，格式：yyyy-MM-dd
	 * @param   $page           当前页码，默认为1　
	 * @param   $pageSize       每页显示的数据条数，默认每页显示1000条数据
	 * @return  array
 	*/
    public function getStatistics($channelId, $currentDay, $page, $pageSize)
    {
        $params['appId'] = config('blw_appId');
        $params['timestamp'] = time()*1000;
        $params['page'] = $page;
        $params['currentDay'] = $currentDay;
        $params['pageSize'] = $pageSize;
        $paramString = self::getASCII($params);
        $params['sign'] = self::getSign($paramString);

        $url = "http://api.polyv.net/live/v2/statistics/".$channelId."/viewlog?appId=".$params['appId']."&currentDay=".$params['currentDay']."&page=".$params['page']."&pageSize=".$params['pageSize']."&timestamp=".$params['timestamp'].'&sign='.$params['sign'];

        $result = httpGet($url);
        $result = json_decode($result,true);

        if ($result['code'] == 200) {
        	return $result;
        }else{
        	return false;
        }
    }

	// 字典顺序（ASCII值大小）升序排列
    public function getASCII($params)
    {
        ksort($params);
        reset($params);
        $str = http_build_query($params, '', '&amp');
        return $str;
    }

    // 签名
    public function getSign($paramString)
    {
    	$appSecret = config('blw_appSecret');
        $paramString = str_replace('=','',$paramString);
        $paramString = str_replace('&amp','',$paramString);
        $string1 = $appSecret.$paramString.$appSecret;
        $sign = strtoupper(md5($string1));
        return $sign;
    }
}

?>